<?php
class ControllerDesignLayout extends AdminController {

	public function index() {
		M('design/layout');
		$this->getList();
	}

	public function insert() {
		M('design/layout');

		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_design_layout->addLayout($this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('design/layout'));
		}

		$this->getForm();
	}

	public function update() {
		M('design/layout');

		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_design_layout->editLayout($this->request->get['layout_id'], $this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('design/layout'));
		}

		$this->getForm();
	}

	public function delete() {

		M('design/layout');

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $layout_id) {
				$this->model_design_layout->deleteLayout($layout_id);
			}

			$this->session->set_flashdata('success', $this->_['text_success']);

			$this->redirect(UA('design/layout'));
		}

		$this->getList();
	}

	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);

		$qf = new Query_filter();

		$layout_total = $this->model_design_layout->getTotalLayouts();
		$this->data['layouts'] = $this->model_design_layout->getLayouts($qf);

		$this->data['pagination'] = $this->pagination($layout_total)->render();

		$this->display('design/layout_list.tpl');
	}

	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);

 		$layout_id = $this->request->get('layout_id');
		if ($layout_id) {
			$this->data['action'] = UA('design/layout/update', 'layout_id=' . $layout_id);
			if (!$this->request->isPost()) {
				$this->vo->data = $this->model_design_layout->getLayout($layout_id);
			}
		} else {
			$this->data['action'] = UA('design/layout/insert');
		}
		
		M('setting/store');
		$this->data['stores'] = $this->model_setting_store->getStores();
		
		//Layout route
		if (isset($this->request->post['layout_route'])) {
			$this->data['layout_routes'] = $this->request->post['layout_route'];
		} elseif ($layout_id) {
			$this->data['layout_routes'] = $this->model_design_layout->getLayoutRoutes($layout_id);
		} else {
			$this->data['layout_routes'] = array();
		}

		$this->display('design/layout_form.tpl');
	}

	protected function validateForm() {

		if (!range_length($this->request->post['name'], 3, 64)) {
			$this->setMessage('error_name', $this->_['error_name']);
			return false;
		}

		return true;
	}

	protected function validateDelete() {

		M('setting/store');
		M('catalog/product');
		M('catalog/category');
		M('catalog/page');

		foreach ($this->request->post['selected'] as $layout_id) {
			if (C('config_layout_id') == $layout_id) {
				$this->setMessage('error_warning', $this->_['error_default']);
				return false;
			}

			$store_total = $this->model_setting_store->getTotalStoresByLayoutId($layout_id);

			if ($store_total) {
				$this->setMessage('error_warning', sprintf($this->_['error_store'], $store_total));
				return false;
			}

			$product_total = $this->model_catalog_product->getTotalProductsByLayoutId($layout_id);

			if ($product_total) {
				$this->setMessage('error_warning', sprintf($this->_['error_product'], $product_total));
				return false;
			}

			$category_total = $this->model_catalog_category->getTotalCategoriesByLayoutId($layout_id);

			if ($category_total) {
				$this->setMessage('error_warning', sprintf($this->_['error_category'], $category_total));
				return false;
			}

			$page_total = $this->model_catalog_page->getTotalPagesByLayoutId($layout_id);

			if ($page_total) {
				$this->setMessage('error_warning', sprintf($this->_['error_page'], $page_total));
				return false;
			}
		}

		return true;
	}
}
?>